﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace wcfServiceLib
{
    [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any, IncludeExceptionDetailInFaults = false)]
    public class Service1 : IService1
    {
        DbContext context = new DbContext("dotnetEntities");


        public Track GetTrack(int id)
        {
            StandardRepository<Track> repo = new StandardRepository<Track>(context);
            var b = repo.GetById(id);
            Track a = new Track() { id = b.id, length = b.length, name = b.name, userid = b.userid, Track1 = b.Track1, Track2 = b.Track2 };

            //context.SaveChanges(); jak chcesz zapisac zmiany w bazie

            return a;
        }

        public bool validCredentials(String login, String password)
        {
            StandardRepository<User> repo = new StandardRepository<User>(context);
            IQueryable<User> usr = repo.GetAll();

            foreach (User user in usr)
            {
                if (user.login.Equals(login) && user.password.Equals(password))
                    return true;
            }

            return false;
        }

        public User getUser(String login)
        {
            StandardRepository<User> repo = new StandardRepository<User>(context);
            IQueryable<User> usr = repo.GetAll();

            foreach (User user in usr)
            {
                if (user.login.Equals(login))
                    return user;
            }

			throw new FaultException<Wyjatek>(new Wyjatek(), "User not found");
        }

        public void addTrack(Track track)
        {
            StandardRepository<Track> repo = new StandardRepository<Track>(context);

            if (track == null)
				throw new FaultException<Wyjatek>(new Wyjatek(), "Can't add empty track");

            repo.Add(track);
            context.SaveChanges();
        }

        public void addPoint(Point point)
        {
            StandardRepository<Point> repo = new StandardRepository<Point>(context);

            if (point == null)
				throw new FaultException<Wyjatek>(new Wyjatek(),"Can't add empty point");

            repo.Add(point);
            context.SaveChanges();
        }

        public void addRun(Run run)
        {
            StandardRepository<Run> repo = new StandardRepository<Run>(context);

            if (run == null)
				throw new FaultException<Wyjatek>(new Wyjatek(), "Can't add empty run");

            repo.Add(run);
            context.SaveChanges();
        }

        public List<Run> getRuns(int id)
        {
            List<Run> runs = new List<Run>();

            StandardRepository<Run> repo = new StandardRepository<Run>(context);
            IQueryable<Run> dbRuns = repo.GetAll();

            foreach (Run run in dbRuns)
            {
                if (run.userid == id)
                    runs.Add(run);
            }

            return runs;
        }

        public List<Track> getTracks(int id)
        {
            List<Track> tracks = new List<Track>();

            StandardRepository<Track> repo = new StandardRepository<Track>(context);
            IQueryable<Track> dbTracks = repo.GetAll();

            foreach (Track track in dbTracks)
            {
                if (track.userid == id)
                    tracks.Add(track);
            }

            return tracks;
        }

        public List<Point> getPoints(int id)
        {
            List<Point> points = new List<Point>();

            StandardRepository<Point> repo = new StandardRepository<Point>(context);
            IQueryable<Point> dbPoints = repo.GetAll();

            foreach (Point point in dbPoints)
            {
                if (point.trackid == id)
                    points.Add(point);
            }

            return points;
        }

        public void registerUser(User user)
        {
            if (user == null)
				throw new FaultException<Wyjatek>(new Wyjatek(), "Can't register empty user");

            StandardRepository<User> repo = new StandardRepository<User>(context);
            IQueryable<User> usr = repo.GetAll();

            foreach (User tuser in usr)
            {
                if (tuser.login.Equals(user.login))
					throw new FaultException<Wyjatek>(new Wyjatek(), "User already exists");
            }

            repo.Add(user);

            context.SaveChanges();
        }

        public String getString(int id)
        {
            return "lalal";
        }
    }
}
